package com.sailfish.springbootdemo.dao.db1;

import com.sailfish.springbootdemo.common.DB1TableName;
import com.sailfish.springbootdemo.pojo.db1.PcbaDeviceResource;
import com.sailfish.springbootdemo.pojo.db1.PcbaSpecialResource;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.type.JdbcType;

import java.util.Date;
import java.util.List;

public interface PcbaSpecialResourceDao {

    /**
     * 根据工厂 + 车间 查询
     *
     * @return
     */
    @Results(id = "pcbaSpecialResourceResultMap", value = {
            @Result(column = "pcba_special_resource_id", property = "pcbaSpecialResourceId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_device_type_id", property = "pcbaDeviceTypeId", jdbcType = JdbcType.INTEGER),
            @Result(column = "sort", property = "sort", jdbcType = JdbcType.INTEGER),
            @Result(column = "create_time", property = "createTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "update_time", property = "updateTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "status", property = "status", jdbcType = JdbcType.INTEGER),
            @Result(column = "begin_time", property = "beginTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "end_time", property = "endTime", jdbcType = JdbcType.DATETIMEOFFSET),
            @Result(column = "pcba_company_id", property = "pcbaCompanyId", jdbcType = JdbcType.INTEGER),
            @Result(column = "pcba_workshop_id", property = "pcbaWorkshopId", jdbcType = JdbcType.INTEGER),
            @Result(column = "notes", property = "notes", jdbcType = JdbcType.VARCHAR),
            @Result(column = "submitter", property = "submitter", jdbcType = JdbcType.INTEGER),
            @Result(column = "auditors", property = "auditors", jdbcType = JdbcType.VARCHAR),
    })
    @Select("<script>" +
            "select * from " + DB1TableName.PCBA_SPECIAL_RESOURCE + " where 1 = 1 " +
            "<if test='pcbaCompanyId != null and pcbaCompanyId != \"\" '>" +
            " and pcba_company_id = #{pcbaCompanyId}" +
            "</if>" +
            "<if test='pcbaWorkshopId != null and pcbaWorkshopId != \"\" '>" +
            " and pcba_workshop_id = #{pcbaWorkshopId}" +
            "</if>" +
            "<if test='pcbaDeviceTypeId != null and pcbaDeviceTypeId != \"\" '>" +
            " and pcba_device_type_id = #{pcbaDeviceTypeId}" +
            "</if>" +
            "<if test='status != null and status != \"\" '>" +
            " and status = #{status}" +
            "</if>" +
            "<if test=\"companyIdList != null and companyIdList.size() > 0 \">\n" +
            "                and pcba_company_id in\n" +
            "                <foreach item=\"item\" index=\"index\" collection=\"companyIdList\"\n" +
            "                         open=\"(\" separator=\",\" close=\")\">\n" +
            "                    #{item}\n" +
            "                </foreach>\n" +
            "            </if>\n" +
            "<if test='begin != null and pageSize != null and pageSize != \"\" '>" +
            " limit #{begin}, #{pageSize}" +
            "</if>" +
            "</script>")
    List<PcbaSpecialResource> getAllByDeviceTypeAndSearch(@Param("pcbaCompanyId") Integer pcbaCompanyId, @Param("pcbaWorkshopId") Integer pcbaWorkshopId, @Param("pcbaDeviceTypeId") Integer pcbaDeviceTypeId, @Param("begin") Integer begin, @Param("pageSize") Integer pageSize, @Param("status") Integer status, @Param("companyIdList") List<Integer> companyIdList);


    @ResultMap(value = "pcbaSpecialResourceResultMap")
    @Select("select * from " + DB1TableName.PCBA_SPECIAL_RESOURCE + " where pcba_special_resource_id = #{pcbaSpecialResourceId} ")
    PcbaSpecialResource getPcbaSpecialResourceById(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);

    /**
     * 删除
     *
     * @param pcbaSpecialResourceId
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_SPECIAL_RESOURCE + " where pcba_special_resource_id = #{pcbaSpecialResourceId}")
    Integer deleteById(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId);

    /**
     * 根据车间id删除
     *
     * @param
     * @return
     */
    @Delete("delete from " + DB1TableName.PCBA_SPECIAL_RESOURCE + " where pcba_workshop_id = #{pcbaWorkshopId}")
    Integer deleteByWorkshopId(@Param("pcbaWorkshopId") Integer pcbaWorkshopId);

    /**
     * 新增
     *
     * @param pcbaSpecialResource
     * @return
     */
    @Insert("insert into " + DB1TableName.PCBA_SPECIAL_RESOURCE + "(status,pcba_device_type_id,sort,create_time,begin_time,end_time,pcba_company_id,pcba_workshop_id,notes,submitter,auditors) values " +
            "(#{pcbaSpecialResource.status},#{pcbaSpecialResource.pcbaDeviceTypeId},#{pcbaSpecialResource.sort},#{pcbaSpecialResource.createTime},#{pcbaSpecialResource.beginTime},#{pcbaSpecialResource.endTime},#{pcbaSpecialResource.pcbaCompanyId},#{pcbaSpecialResource.pcbaWorkshopId},#{pcbaSpecialResource.notes},#{pcbaSpecialResource.submitter},#{pcbaSpecialResource.auditors})")
    // 显式设置主键
    @Options(useGeneratedKeys = true, keyProperty = "pcbaSpecialResource.pcbaSpecialResourceId")
    Integer addPcbaSpecialResource(@Param("pcbaSpecialResource") PcbaSpecialResource pcbaSpecialResource);

    /**
     * 更新
     *
     * @param pcbaSpecialResource
     * @return
     */
    @Update("<script>" +
            "update " + DB1TableName.PCBA_SPECIAL_RESOURCE +
            "<set>" +
            "<if test='pcbaSpecialResource.updateTime != null '>" +
            " update_time = #{pcbaSpecialResource.updateTime}," +
            "</if>" +
            "<if test='pcbaSpecialResource.status != null and pcbaSpecialResource.status != \"\" '>" +
            " status = #{pcbaSpecialResource.status}," +
            "</if>" +
            "<if test='pcbaSpecialResource.pcbaDeviceTypeId != null and pcbaSpecialResource.pcbaDeviceTypeId != \"\" '>" +
            " pcba_device_type_id = #{pcbaSpecialResource.pcbaDeviceTypeId}," +
            "</if>" +
            "<if test='pcbaSpecialResource.pcbaCompanyId != null and pcbaSpecialResource.pcbaCompanyId != \"\" '>" +
            " pcba_company_id = #{pcbaSpecialResource.pcbaCompanyId}," +
            "</if>" +
            "<if test='pcbaSpecialResource.pcbaWorkshopId != null and pcbaSpecialResource.pcbaWorkshopId != \"\" '>" +
            " pcba_workshop_id = #{pcbaSpecialResource.pcbaWorkshopId}," +
            "</if>" +
            "<if test='pcbaSpecialResource.beginTime != null '>" +
            " begin_time = #{pcbaSpecialResource.beginTime}," +
            "</if>" +
            "<if test='pcbaSpecialResource.endTime != null '>" +
            " end_time = #{pcbaSpecialResource.endTime}," +
            "</if>" +
            "<if test='pcbaSpecialResource.sort != null and pcbaSpecialResource.sort != \"\" '>" +
            " sort = #{pcbaSpecialResource.sort}," +
            "</if>" +
            "<if test='pcbaSpecialResource.notes != null '>" +
            " notes = #{pcbaSpecialResource.notes}," +
            "</if>" +
            "<if test='pcbaSpecialResource.submitter != null and pcbaSpecialResource.submitter != \"\" '>" +
            " submitter = #{pcbaSpecialResource.submitter}," +
            "</if>" +
            "<if test='pcbaSpecialResource.auditors != null and pcbaSpecialResource.auditors != \"\" '>" +
            " auditors = #{pcbaSpecialResource.auditors}," +
            "</if>" +
            "</set>" +
            " where pcba_special_resource_id = #{pcbaSpecialResource.pcbaSpecialResourceId}" +
            "</script>")
    Integer updatePcbaSpecialResource(@Param("pcbaSpecialResource") PcbaSpecialResource pcbaSpecialResource);

    /**
     * 审核
     *
     * @param pcbaSpecialResourceId
     * @param notes
     * @param status
     * @return
     */
    @Update("update " + DB1TableName.PCBA_SPECIAL_RESOURCE + " set notes = #{notes}, status = #{status}, update_time = #{updateTime} where pcba_special_resource_id = #{pcbaSpecialResourceId}")
    Integer auditPcbaSpecialResource(@Param("pcbaSpecialResourceId") Integer pcbaSpecialResourceId, @Param("notes") String notes, @Param("status") Integer status, @Param("updateTime") Date updateTime);
}
